<?php
class Rulemanager_Model_Pagedecision implements Rulemanager_Model_Mainable{
    
    private $core_controller_csrftoken;
    private $action_handler;
    
    public function main(array $variables = null){
        switch($variables['get']['page']){
            case 'dodaj-role':
                return $this->add_rule($variables);
                break;
            case 'wstaw-role':
                return $this->insert_rule($variables);
                break;
            case 'edytuj-role':
                return $this->edit_rule($variables);
                break;
            case 'aktualizuj-role':
                return $this->update_rule($variables);
                break;
            case 'usun-role':
                return $this->delete_rule($variables);
                break;
            default :
                return $this->default_action($variables);
                break;
        }
    }
    
    private function delete_rule(array $variables = null){
        $this->action_handler = new Rulemanager_Model_Deleterule;
        return $this->action_handler->main($variables);
    }
    
    private function update_rule(array $variables = null){
        $this->action_handler = new Rulemanager_Model_Updaterule;
        return $this->action_handler->main($variables);
    }
    
    private function edit_rule(array $variables = null){
        $page = $this->get_other_data($variables);
        $this->action_handler = new Rulemanager_Data_Getcurrentrule;
        $page['content'] = $this->action_handler->main($variables);
        return $page;
    }
    
    private function insert_rule(array $variables = null){
        $this->action_handler = new Rulemanager_Model_Insertrule;
        return $this->action_handler->main($variables);
    }

    private function add_rule(array $variables = null){
        $page = $this->get_other_data($variables);
        return $page;
    }
    
    private function get_other_data(array $variables = null){
        $this->core_controller_csrftoken = new Core_Controller_Csrftoken;
        $page['csrf_token'] = $this->core_controller_csrftoken->main($variables);
        $page['template'] = WEB_ROOT_FOLDER."rulemanager/views/addrule.php";
        return $page;
    }
    
    private function default_action(array $variables = null){
        unset($variables['get']);
        $this->action_handler = new Rulemanager_Model_Rulemanager;
        return $this->action_handler->main($variables);
    }
}
